%2011-01-28 Plots of training and unlabeled and test data.
% For finding good datasets and analysing datasets too.

%INPUTS
%functionValTrain               (two_step_gurobi.m)
%Lambda                         (two_step_gurobi.m)
%numFeatures                    (main.m)
%trainingdata                   (main.m)
%testdata                       (main.m)
%flag_reverseEngg_plots         (main.m)
%numTrain                       (main.m)

%OUTPUTS
%index_training_negatives     %Can't be initialized
%index_training_positives     %Can't be initialized
%index_test_negatives         %Can't be initialized
%index_test_positives         %Can't be initialized
probability_threshold       = 0.005;
probability_training        = zeros(numTrain,1);
functionValTest             = zeros(length(testdata(:,1)),1);
testloss                    = -1;


%For debugging using plots of 2 features vs the output for training data
%Finding pts whose predicted labels are 1/0 depending on the threshold
probability_training=1./(1+exp(-functionValTrain));
index_training_negatives=find(probability_training <= probability_threshold);
index_training_positives=find(probability_training  > probability_threshold);
% Plotting 2 features of the training data
if(flag_plotFeatures2Dtraining == 1)
    figure;
    scatter(trainingdata(index_training_negatives,index_featureA),trainingdata(index_training_negatives,index_featureB),'r.'); hold on;
    scatter(trainingdata(index_training_positives,index_featureA),trainingdata(index_training_positives,index_featureB),'b.'); hold off;
    axis equal; legend('red:negative', 'blue:positive');
end

%check test error given the test labels.
functionValTest=testdata(:,1:numFeatures)*Lambda(2:end) + Lambda(1);   % This is the model
testloss =  sum(log(1+exp(-(testdata(:,end).*functionValTest))));%(1/length(testdata(:,1)))*

%For debugging using plots of 2 features vs the output for test data
% Finding pts whose predicted labels are 1/0 depending on the threshold
Proba1test=1./(1+exp(-functionValTest));
index_test_negatives=find(Proba1test <= probability_threshold);
index_test_positives=find(Proba1test >  probability_threshold);
% Plotting 2 features of the training data with the decision boundary.
if(flag_plotFeatures2Dtest == 1)
    figure;
    scatter(testdata(index_test_negatives,index_featureA),testdata(index_test_negatives,index_featureB),'r.'); hold on;
    scatter(testdata(index_test_positives,index_featureA),testdata(index_test_positives,index_featureB),'b.'); hold off;
    axis equal; legend('red:negative', 'blue:positive');
end


%Reverse engineering
if(flag_reverseEngg_plots==1)
   indextrue_train  =  find(trainingdata(:,end)==1);
   indexfalse_train =  find(trainingdata(:,end)==-1);
   figure;
   subplot(4,2,1); hist(trainingdata(indextrue_train,1));
   subplot(4,2,2); hist(trainingdata(indexfalse_train,1));
   subplot(4,2,3); hist(trainingdata(indextrue_train,2));
   subplot(4,2,4); hist(trainingdata(indexfalse_train,2));
   subplot(4,2,5); hist(trainingdata(indextrue_train,3));
   subplot(4,2,6); hist(trainingdata(indexfalse_train,3));
   subplot(4,2,7); hist(trainingdata(indextrue_train,4));
   subplot(4,2,8); hist(trainingdata(indexfalse_train,4));
end


